package com.hoki.zj.hrm.consumer;

import com.hoki.zj.hrm.dto.EmailDto;
import com.hoki.zj.hrm.dto.SMSDto;
import com.hoki.zj.hrm.dto.SMsgDto;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import static com.hoki.zj.hrm.constant.MQMessageConst.QUEUE_NAME_EMAIL;
import static com.hoki.zj.hrm.constant.MQMessageConst.QUEUE_NAME_SMS;
import static com.hoki.zj.hrm.constant.MQMessageConst.QUEUE_NAME_SMSG;

@Component
@Slf4j
public class MQMessageConsumer {

    /**
     * 1.消费短信消息
     * @param smsDto smsDto
     * @param channel 通道
     * @param message 消息内容
     *
     */
    @RabbitListener(queues = QUEUE_NAME_SMS, containerFactory = "rabbitListenerContainerFactory")
    public void receiveSms(@Payload SMSDto smsDto, Message message, Channel channel) throws Exception {
        log.info("手机号: {}, 消息内容: {}", smsDto.getPhoneNum(), smsDto.getContent());
        long deliveryTag = message.getMessageProperties().getDeliveryTag(); // 获取标签
        channel.basicAck(deliveryTag, false); // 手动签收
        channel.close();// 关闭通道
    }

    /**
     * 2.消费邮件消息
     * @param emailDto emailDto
     * @param channel 通道
     * @param message 消息内容
     *
     */
    @RabbitListener(queues = QUEUE_NAME_EMAIL, containerFactory = "rabbitListenerContainerFactory")
    public void receiveEmail(@Payload EmailDto emailDto, Message message, Channel channel) throws Exception {
        log.info("邮箱: {}, 标题: {}, 消息内容: {}", emailDto.getEmailAddress(), emailDto.getTitle(), emailDto.getContent());
        long deliveryTag = message.getMessageProperties().getDeliveryTag(); // 获取标签
        channel.basicAck(deliveryTag, false); // 手动签收
        channel.close();// 关闭通道
    }

    /**
     * 3.消费站内信消息
     * @param sMsgDto sMsgDto
     * @param channel 通道
     * @param message 消息内容
     *
     */
    @RabbitListener(queues = QUEUE_NAME_SMSG, containerFactory = "rabbitListenerContainerFactory")
    public void receiveEmail(@Payload SMsgDto sMsgDto, Message message, Channel channel) throws Exception {
        log.info("用户id: {}, 标题: {}, 消息内容: {}", sMsgDto.getUserId(), sMsgDto.getTitle(), sMsgDto.getContent());
        long deliveryTag = message.getMessageProperties().getDeliveryTag(); // 获取标签
        channel.basicAck(deliveryTag, false); // 手动签收
        channel.close();// 关闭通道
    }
}
